/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.vdabantwerpen.gow.servlets;

import be.vdabantwerpen.gow.entities.GOWUser;
import be.vdabantwerpen.gow.entities.Weight;
import be.vdabantwerpen.gow.exceptions.WeightNotFoundException;
import be.vdabantwerpen.gow.services.ActionService;
import be.vdabantwerpen.gow.services.ActionServiceImpl;
import be.vdabantwerpen.gow.services.UserService;
import be.vdabantwerpen.gow.services.UserServiceImpl;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//Test3
/**
 *
 * @author tim.vandenlangenberg
 * @version 1.0
 */
public class ConfigServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String VIEW = "/WEB-INF/JSP/config.jsp";
    private static final String INDEX = "/";
    private static final int MAX_WEIGHT = 100;
    
    private static final ActionService actionService = new ActionServiceImpl();
    private static final UserService userService = new UserServiceImpl();
    
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        com.google.appengine.api.users.UserService gUserService = 
                UserServiceFactory.getUserService();
        com.google.appengine.api.users.User currentUser = gUserService.getCurrentUser();
        GOWUser user = userService.read(Integer.parseInt(currentUser.getUserId()));
        
        if(user != null && user.getRole()){
            try {
                request.setAttribute("weights", actionService.findWeights());
            }
            catch(WeightNotFoundException wnfe){
                request.setAttribute("fouten", Arrays.asList(wnfe));
            }
            request.setAttribute("maxWeight", MAX_WEIGHT);
            request.getRequestDispatcher(VIEW).forward(request, response);
        }
        else {
            response.sendRedirect(INDEX);
        }
    }
    
    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<String> errors = new ArrayList();
        
        String actionType = request.getParameter("actionType");
        String weightAsString = request.getParameter("weight");
        int weight = 0;
        if(actionType == null || actionType.isEmpty()){
            errors.add("Ongeldige actie");
        }
        if(weightAsString == null || weightAsString.isEmpty()){
            errors.add("Ongeldige weight");
        }
        else {
            try {
                weight = Integer.parseInt(weightAsString);
            }
            catch(NumberFormatException nfe){
                errors.add("Weight moet een getal zijn");
            }
        }
        
        if(errors.isEmpty()){
            actionService.updateWeight(new Weight(actionType, weight));
            response.sendRedirect("/config.htm");
        }
        else {
            request.setAttribute("weights", actionService.findWeights());
            request.setAttribute("maxWeight", MAX_WEIGHT);
            request.setAttribute("fouten", errors);
            request.getRequestDispatcher(VIEW).forward(request, response);
        }
    }
    
}
